package com.mtiiot.server.netty;


import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import com.mtiiot.util.Constants;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

public class NettyServer {
	private static Logger logger = LogManager.getLogger(NettyServer.class);
	
	public void bing(int port) throws Exception{
		EventLoopGroup bossGroup = new NioEventLoopGroup();
		EventLoopGroup workerGroup = new NioEventLoopGroup();
		try {
			ServerBootstrap b = new ServerBootstrap();
			b.group(bossGroup, workerGroup);
			b.channel(NioServerSocketChannel.class);
			b.option(ChannelOption.SO_BACKLOG, Constants.config.getBacklog());
			b.childHandler(new ChildChannelHandler());
			logger.info("server is running,start to monitor the client connect");

			//绑定端口
			ChannelFuture f = b.bind(port).sync();
			//等待服务端监听端口关闭
			f.channel().closeFuture().sync();
		} finally{
			bossGroup.shutdownGracefully();
			workerGroup.shutdownGracefully();
		}
	}
//	public static void main(String[] args) {
//		boolean f=StoreManager.hPut("lcy", "helloworld");
//		if(f){
//			String value=StoreManager.hGet("lcy");
//			System.out.println(value);
//			return;
//		}
//		System.out.println("put 失败");
//	}
}
